<template>
  <div v-show="show" class="totop-container" @click="handleClick">ToTop</div>
</template>

<script>
import debounce from '../utils/debounce.js';

export default {
  data() {
    return {
      show: false,
    };
  },
  created() {
    this.handleMainScrollDebounce = debounce(this.handleMainScroll);
    this.$bus.$on("mainScroll", this.handleMainScrollDebounce);
  },
  destroyed() {
    this.$bus.$off("mainScroll", this.handleMainScrollDebounce);
  },
  methods: {
    handleMainScroll(container) {
      if (!container) {
        this.show = false;
        return;
      }
      const range = 1000;
      this.show = container.scrollTop > range;
    },
    handleClick() {
      this.show = false;
      this.$bus.$emit("setMainScroll", 0);
    },
  },
};
</script>

<style scoped lang="less">
@import "../styles/color.less";

.totop-container {
  width: 70px;
  height: 70px;
  border-radius: 50%;
  background-color: @primary;
  color: #fff;
  user-select: none;
  position: fixed;
  right: 50px;
  bottom: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
</style>